package com.example.zookeeper.lock.task;

import com.example.zookeeper.lock.util.DefaultExecutor;
import com.example.zookeeper.lock.util.ZookeeperUtils;
import com.example.zookeeper.lock.watch.WatchCallBack;
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Callable;

/**
 * @author Vicente
 * @version 1.0
 * @date 2020/3/9 15:17
 */
public class WorkTask implements Callable<String> {
    private Logger logger = LoggerFactory.getLogger(WorkTask.class);

    public String call() throws Exception {
        ZooKeeper zooKeeper = ZookeeperUtils.getZooKeeper();
        WatchCallBack watchCallBack = new WatchCallBack();
        watchCallBack.setThreadName(Thread.currentThread().getName());
        watchCallBack.setZooKeeper(zooKeeper);
        watchCallBack.tryLock();
        logger.warn(Thread.currentThread().getName() + " 获取锁成功，开始工作...");
        Thread.sleep(3000);
        logger.warn(Thread.currentThread().getName() + " 工作结束，释放锁...");
        watchCallBack.unLock();
        return "SUCCESS";
    }
}
